<template>
  <div class="container">
    <div class="mb10">
      <el-text class="mx-1"> 查询条件：</el-text>
    </div>
    <el-row :gutter="10">
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-select v-model.trim="searchCondition.content_type" placeholder="带货场景" clearable>
          <el-option v-for="item in contentType" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-select v-model.trim="searchCondition.status" placeholder="计划状态" clearable>
          <el-option v-for="item in cpsStatus" :label="item.dictLabel" :value="item.dictValue" :key="item.id"></el-option>
        </el-select>
      </el-col>
      <el-col :xl="6" :lg="6" :md="12" :sm="24">
        <el-space wrap>
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-space>
      </el-col>
    </el-row>
    <div class="mb10">
      <el-text class="mx-1"> 查询结果：</el-text>
    </div>
    <el-space wrap class="mb20">
      <el-button type="primary" :icon="Plus" :disabled="disabledbtn" @click="changeFormStatus('add')" v-permission="'add'">新增</el-button>
    </el-space>
    <el-table :data="tableList" border class="table" row-key="id" ref="multipleTableRef" v-loading="loading" :reserve-selection="true" @row-dblclick="rowDblclick">
      <el-table-column type="index" align="center"> </el-table-column>
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="plan_id" label="计划id" align="center"> </el-table-column>
      <el-table-column prop="plan_name" label="计划名称" align="center"> </el-table-column>
      <el-table-column prop="merchant_phone" label="计划联系人手机号" align="center"> </el-table-column>
      <el-table-column prop="content_type_name" label="带货场景" align="center"> </el-table-column>
      <el-table-column prop="commission_duration" label="佣金有效期/天" align="center"> </el-table-column>
      <el-table-column prop="start_time" label="上传投稿视频的开始时间" align="center" :formatter="(row: ITargetedCommissionPlanList) => handleVerifyFormatDate(row.start_time)"> </el-table-column>
      <el-table-column prop="end_time" label="上传投稿视频的结束时间" align="center" :formatter="(row: ITargetedCommissionPlanList) => handleVerifyFormatDate(row.end_time)"> </el-table-column>
      <el-table-column prop="status" label="计划状态" align="center">
        <template #default="scope">
          <div class="w-tag">
            <el-tag :type="scope.row.status === 1 ? 'success' : scope.row.status === 2 ? 'warning' : scope.row.status === 3 ? 'danger' : 'success'">{{ scope.row.status_name }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" align="center" :formatter="(row: ITargetedCommissionPlanList) => handleVerifyFormatDate(row.create_time)"> </el-table-column>
      <el-table-column label="操作" width="210" align="center">
        <template #default="scope">
          <div class="control-flex">
            <el-button type="primary" link :icon="View" @click="handleViewEdit(scope.row, 'view')">查看</el-button>
            <el-button type="primary" link :icon="EditPen" v-permission="'edit'" @click="handleViewEdit(scope.row, 'edit')">编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        @update:current-page="handleCurrentChange"
        @update:page-size="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="searchCondition.size"
        :current-page="searchCondition.current"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        :total="total"
        :hide-on-single-page="false">
      </el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import { View, EditPen, Search, Refresh, Plus } from "@element-plus/icons-vue";
import { handleVerifyFormatDate } from "@/utils/utils";
import type { ViewMode, ViewModeFilter } from "@/types/common";
import { useState } from "@/hooks/useState.ts";
import type { IDictData } from "@/types/dict";
import { getDouyinOrientedCpsInfo } from "@/utils/api/douyinOrientedCps";
import type { ISearchTargetedCommissionPlan, ITargetedCommissionPlanList } from "@/types/douyinManager/targetedCommissionPlan";
interface Props {
  contentType: Array<IDictData>;
  cpsStatus: Array<IDictData>;
}
const { contentType, cpsStatus } = defineProps<Props>();
const { disabledbtn, loading, toggleState } = useState();

const emit = defineEmits<{
  handleAdd: [code: ViewMode];
  handleViewEdit: [id: number, code: ViewModeFilter];
}>();
/* 暴露一个方法给父组件使用 */
defineExpose({
  /* 这一步关键，在setup语法糖中使用时 */
  getTableInfo
});
const multipleTableRef = useTemplateRef("multipleTableRef"); /* 列表组件的引用 */

const total = ref(0);
const searchCondition = reactive<ISearchTargetedCommissionPlan>({
  content_type: "",
  status: "",
  size: 10,
  current: 1
});
const tableList = ref<Array<ITargetedCommissionPlanList>>([]);

onMounted(() => {
  getTableInfo();
});
/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 10;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.content_type = "";
  searchCondition.status = "";

  searchCondition.current = 1;
  searchCondition.size = 10;

  getTableInfo();
}

/**
 * 切换表单状态（ 添加、编辑、查看 ，隐藏none ）
 */
function changeFormStatus(state: ViewMode) {
  // formStatus.value = state;
  emit("handleAdd", state);
}

/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getDouyinOrientedCpsInfo<ITargetedCommissionPlanList, ISearchTargetedCommissionPlan>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}

/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.current = 1;
  searchCondition.size = val;
  getTableInfo();
}

/* 触发处理 查看编辑事件 */
function handleViewEdit(row: ITargetedCommissionPlanList, code: ViewModeFilter) {
  emit("handleViewEdit", row.id, code);
}

/* 双击当前行 */
function rowDblclick(row: ITargetedCommissionPlanList) {
  multipleTableRef.value!.toggleRowSelection(row);
}
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-col {
  border-radius: 4px;
}

.mb10 {
  margin-bottom: 10px;
}
/* 关键：列表右侧操作按钮的对其 */
:deep(.el-space__item) {
  line-height: 15px;
}

.el-button.is-text + .el-button.is-text {
  margin-left: 0;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}
.textAlignCenter {
  text-align: center;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

.w-tag .el-tag {
  margin: 2px 5px;
}
</style>
